﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace EBA.Ex
{
    public static class XElementEx
    {
        public static string GetAttributeValue(this XElement element, string name)
        {
            if (element == null)
            {
                return null;
            }
            else
            {
                var attr = element.Attribute(name);
                if (attr == null)
                {
                    return null;
                }
                else
                {
                    return attr.Value;
                }
            }
        }

        //public static XElement Serialize<TEntity>(this TEntity entity)
        //{
        //    MemoryStream memStream = new MemoryStream();
        //    using (XmlTextWriter textWriter = new XmlTextWriter(memStream, Encoding.Unicode))
        //    {
        //        System.Xml.Serialize.XmlSerializer serializer = new System.Xml.Serialize.XmlSerializer(typeof(TEntity));
        //        serializer.Serialize(textWriter, entity);

        //        memStream = textWriter.BaseStream as MemoryStream;
        //    }
        //    if (memStream != null)
        //    {
        //        new XElement(
        //    }

        //    else
        //    {
        //        return null;
        //    }
        //}

        //public static TEntity Deserialize<TEntity>(this XElement element)
        //{
        //    if (element == null)
        //    {
        //        return default(TEntity);
        //    }

        //    using (MemoryStream memStream = new MemoryStream(Encoding.Unicode.GetBytes(element.ToString())))
        //    {
        //        var serializer = new System.Xml.Serialize.XmlSerializer(typeof(TEntity));
        //        return (TEntity)serializer.Deserialize(memStream);
        //    }
        //}
    }
}
